package com.xspace.android.xafcommon.db.dbbiz;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.db.dbsub.MyDBHelper;
import com.xspace.android.xafcommon.util.LogUtil;

public class PreferencesDBBiz extends BaseSql {
    private String tableName = "Preferences";
    private String key = "key";
    private String value = "value";
    private SQLiteDatabase sqldb;
    private MyDBHelper helpDb;


    public PreferencesDBBiz(MyDBHelper db) {
        super(db);
        helpDb = db;
    }

    public PreferencesDBBiz() {
    }

    protected void checkDb() {
        if (sqldb == null || !sqldb.isOpen()) {
            sqldb = helpDb.getWritableDatabase();
        }
    }


    @Override
    public void create(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + tableName
                + " (" + key
                + " text," + value + " text);");
    }

    public void savePreferences(String key, String value) {
        String selection = this.key + "= ? ";
        String[] selectionArgs = new String[]{key};
        values = new ContentValues();
        values.put(this.key, key);
        values.put(this.value, value);
        Cursor cur = null;
        try {
            checkDb();
            cur = sqldb.query(tableName, null, selection, selectionArgs, null, null, null);
            if (cur != null && cur.getCount() > 0) {
                // 存在对应key更新
                int result = sqldb.update(tableName, values, selection, selectionArgs);
                LogUtil.d("update result " + result);
            } else {
                long insertResult = sqldb.insert(tableName, null, values);
                LogUtil.d("insert result " + insertResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeDbAndCursor(cur);
        }
    }

    private void closeDbAndCursor(Cursor cur) {
        if (cur != null) {
            cur.close();
            cur = null;
        }
    }


    public String queryPreferences(String key) {
        selection = this.key + "=?";
        selectionArgs = new String[]{key};
        // LogUtil.d("keyName=="+key);
        String value = "";
        Cursor cur = null;
        try {
            // db.open();
            String[] columns = {this.key, this.value};
            checkDb();
            cur = sqldb.query(tableName, columns, selection, selectionArgs, null, null, null);
            // LogUtil.d("cur=="+cur.getCount());
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        value = cur.getString(1);
                        // LogUtil.d("cur value=="+value);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cur != null) {
                try {
                    cur.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            // if(db != null){
            // try {
            // db.close();
            // } catch (Exception e2) {
            // MobclickAgent.reportError(MyApplication.mContext, e2);
            // e2.printStackTrace();
            // }
            //
            // }
        }
        return value;
    }

    public void dropTable(SQLiteDatabase db) {
        db.execSQL(TB_DELETE + tableName);
    }

    @Override
    public void upgrade(SQLiteDatabase arg0) {

    }

}
